//
//  TracePrototypeParser.cpp
//  airfight
//
//  Created by aaron more on 12-9-19.
//  Copyright (c) 2012年 moonriver. All rights reserved.
//

#include <iostream>
#include "TracePrototypeParser.h"
#include "Trace.h"

TracePrototypeParser::TracePrototypeParser()
:prototypes(NULL)
,currentTrace(NULL)
,currentMove(NULL)
{
    
}

TracePrototypeParser::~TracePrototypeParser()
{
    CC_SAFE_RELEASE(currentMove);
    CC_SAFE_RELEASE(currentTrace);
    CC_SAFE_RELEASE(prototypes);
}

TracePrototypeParser * TracePrototypeParser::formatWithXMLFile(const char *xmlFile)
{
    TracePrototypeParser *pRet = new TracePrototypeParser();
    if(pRet->initWithXMLFile(xmlFile))
    {
        pRet->autorelease();
        return pRet;
    }
    CC_SAFE_DELETE(pRet);
    return NULL;
}

void TracePrototypeParser::internalInit(const char* tmxFileName)
{
    prototypes = TracePrototypes::getInstance();
    prototypes->retain();
    if (tmxFileName != NULL)
    {
        m_sXMLFileName = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(tmxFileName);
    }
}

void TracePrototypeParser::startElement(void *ctx, const char *name, const char **atts)
{
    
    CC_UNUSED_PARAM(ctx);
    std::string elementName = (char*)name;
    std::map<std::string, std::string> *attributeDict = new std::map<std::string, std::string>();
    if(atts && atts[0])
    {
        for(int i = 0; atts[i]; i += 2) 
        {
            std::string key = (char*)atts[i];
            std::string value = (char*)atts[i+1];
            attributeDict->insert(std::pair<std::string, std::string>(key, value));
        }
    }
    if("trace" == elementName )
    {
        Trace *trace = new Trace();
        //        int i = 0;
        //        sscanf(valueForKey("id", attributeDict), "%d", &i);
        trace->setTraceID((int)atoi(valueForKey("id", attributeDict)));
        
        if (!currentTrace) 
        {
            currentTrace = trace;
        }
    }
    else if("move" == elementName)
    {
        if(currentTrace)
        {
            Move* move = new Move();
            move->setStep((int)atoi(valueForKey("step", attributeDict)));
            move->setEndPoint((float)atof(valueForKey("mpx", attributeDict)), (float)atof(valueForKey("mpy", attributeDict)));
            move->setDuration((float)atof(valueForKey("duration", attributeDict)));
            move->setAngle((int)atoi(valueForKey("angle", attributeDict)));
            move->setMoveType((int)atoi(valueForKey("type", attributeDict))); 
            move->setWish((int)atoi(valueForKey("wish", attributeDict)));
            if ( !currentMove )
            {
                currentMove = move;
            }
            
        }
    }
    else if("control" == elementName)
    {
        if(currentMove)
        {
            Control* control = new Control();
            control->setControlPoint((float)atof(valueForKey("cpx", attributeDict)), (float)atof(valueForKey("cpy", attributeDict)));
            currentMove->addControl(control);
            control->release();
        }
    }
}

void TracePrototypeParser::endElement(void *ctx, const char *name)
{
    CC_UNUSED_PARAM(ctx);
    std::string elementName = (char*)name;
    if("trace" == elementName)
    {
        prototypes->addTrace(currentTrace);
        currentTrace->release();
        currentTrace = NULL;
    }
    else if("move" == elementName)
    {
        if(currentTrace)
        {
            currentTrace->addMove(currentMove);
            currentMove->release();
            currentMove = NULL;
        }
    }
}

void TracePrototypeParser::textHandler(void *ctx, const char *s, int len)
{
    
}
